package com.yunti.springboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.mapper.SchoolMapper;
import com.yunti.springboot.po.School;
import com.yunti.springboot.service.SchoolService;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.SchoolReq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author kmustleng
 * @since 2022-12-09
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {
    @Resource
    SchoolMapper mapper;

    @Override
    public Result getSchoolListPage(SchoolReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(0);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page page = new Page<>(req.getCurrent(), req.getSize());
        return Result.success(page.setRecords(mapper.getSchoolListPage(req,page)));
    }

    @Override
    public Integer insertOne(School school) {
        return mapper.insert(school);
    }

    @Override
    public Result addSchool(SchoolReq req) {
        if(StrUtil.isBlank(req.getName())){
            return Result.error("新增学校失败！,学校名不能为空","新增学校失败！,学校名不能为空");
        }
        QueryWrapper<School> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",req.getName());
        List<School> schoolList = mapper.selectList(queryWrapper);
        if(schoolList.size()>0){
            return Result.error("新增学校失败！该学校已存在","新增学校失败！该学校已存在");
        }
        School school = new School()
                .setName(req.getName())
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        if(mapper.insert(school)>0){
            return Result.success("新增学校成功");
        }
        return Result.error("新增学校失败！","新增学校失败！");
    }

    @Override
    public Result editSchool(SchoolReq req) {
        if(StrUtil.isBlank(req.getName())){
            return Result.error("","新增学校失败！,学校名不能为空");
        }
        QueryWrapper<School>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",req.getName());
        List<School> schoolList = mapper.selectList(queryWrapper);
        if(schoolList.size()>0){
            return Result.error("新增学校失败！该学校已存在","更改学校失败！该学校已存在");
        }
        School school = new School()
                .setName(req.getName())
                .setCreateTime(new Date())
                .setUpdateTime(new Date())
                .setId(req.getId());
        if(mapper.updateById(school)>0){
            return Result.success("更改学校成功");
        }
        return Result.error("更改学校失败！","更改学校失败！");
    }

    @Override
    public Result delSchool(SchoolReq req) {
        if(req.getId() == null){
            return Result.error("","删除学校失败！,学校id不能为空");
        }
        if(mapper.deleteById(new School().setId(req.getId()))>0){
            return Result.success("删除学校成功");
        }
        return Result.error("删除学校失败！");
    }
}
